<?php

namespace App\Models;

use App\Models\Order\Item;
use App\Models\Order\Order;
use Illuminate\Database\Eloquent\Model;

class Teacher extends Model
{
    protected $fillable = ['name', 'tel', 'remark'];

    public $table = 'teacher';

    public function getTotalAttribute($value)
    {
        return $this->attributes['total'] = $this->orderNum();
    }

    public function getRadioAttribute($value)
    {
        return $this->attributes['radio'] = $this->orderSum() / 100;
    }

    public function orderNum()
    {
        $orders = Order::with('item.video')->where('state', Order::ORDER_PAY)->get();
        $orders = $orders->filter(function (Order $order) {
            return $order->item->video->teacher_id === $this->id;
        });
        return $orders->sum('total') ?? 0;
    }

    public function orderSum()
    {
        return Order::where('state', Order::ORDER_PAY)->sum('total');
    }
}
